/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package com.zgw.his.design.model.property.edit;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.editor.AbstractTextPropertyEditor;
import org.eclipse.wb.internal.core.model.property.editor.PropertyEditor;
import org.eclipse.wb.internal.core.model.property.editor.TextDialogPropertyEditor;
import org.eclipse.wb.internal.core.model.property.editor.presentation.ButtonPropertyEditorPresentation;
import org.eclipse.wb.internal.core.model.property.editor.presentation.PropertyEditorPresentation;
import org.eclipse.wb.internal.core.model.property.editor.string.StringPropertyEditor;
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
import org.eclipse.wb.internal.core.xml.model.property.GenericProperty;

import com.zgw.his.common.ValueUtil;
import com.zgw.his.design.model.property.edit.dialog.CodeDialog;
import com.zgw.his.design.resource.BaseResource;

/**
 * 
 * 标识属性编辑 {@link PropertyEditor} for selecting model of {@link Object} in XWT.
 * 
 * @author scheglov_ke
 * @coverage XWT.model.property.editor
 */
public final class CodePropertyEditor extends AbstractTextPropertyEditor {
	// //////////////////////////////////////////////////////////////////////////
	//
	// Instance
	//
	// //////////////////////////////////////////////////////////////////////////
	public static final PropertyEditor INSTANCE = new CodePropertyEditor();

	// //////////////////////////////////////////////////////////////////////////
	//
	// Presentation
	//
	// //////////////////////////////////////////////////////////////////////////
	private final PropertyEditorPresentation m_presentation = new ButtonPropertyEditorPresentation() {
		@Override
		protected void onClick(PropertyTable propertyTable, Property property)
				throws Exception {
			openDialog(propertyTable, property);
		}
	};

	@Override
	public PropertyEditorPresentation getPresentation() {
		return m_presentation;
	}

	// //////////////////////////////////////////////////////////////////////////
	//
	// Editing
	//
	// //////////////////////////////////////////////////////////////////////////
	@Override
	protected String getEditorText(Property property) throws Exception {
		Object value = property.getValue();
		if (value instanceof String) {
			return (String) value;
		}
		return null;
	}

	@Override
	protected boolean setEditorText(Property property, String text)
			throws Exception {
		property.setValue(text);
		return true;
	}

	// //////////////////////////////////////////////////////////////////////////
	//
	// Presentation
	//
	// //////////////////////////////////////////////////////////////////////////
	@Override
	public String getText(Property property) throws Exception {
		Object value = property.getValue();
		if (value instanceof String) {
			String codeText = BaseResource.getElementName((String) value);
			if (ValueUtil.hasValue(codeText)) {
				return codeText + "[" + value + "]";
			}
			return (String) value;
		}
		return null;
	}

	protected void openDialog(PropertyTable propertyTable, Property property) {
		try {
			String code = getEditorText(property);
			CodeDialog dlg = new CodeDialog(Display.getCurrent()
					.getActiveShell(), SWT.NONE, code);
			String result = dlg.open();
			if (ValueUtil.hasValue(result) && !result.equals(code)) {
				property.setValue(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
